/*
 * Copyright (C) 2007 Eskil Bylund
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

using System;
using System.IO;

using ICSharpCode.SharpZipLib.BZip2;
using ICSharpCode.SharpZipLib.GZip;

namespace DCSharp
{
	/// <summary>
	/// Helper methods for (de)compressing data in streams.
	/// </summary>
	public static class FileUtil
	{
		#region Compression

		public enum Compression
		{
			BZip2,
			GZip
		}

		/// <summary>
		/// Returns a stream that can be used to read the compressed data in a stream.
		/// </summary>
		/// <param name="inStream">The stream with the compressed data.</param>
		/// <param name="compression">The compression type that was used.</param>
		/// <returns>The new stream.</returns>
		public static Stream GetDecompressionStream(Stream inStream,
			Compression compression)
		{
			// TODO: Require a version of SharpZipLib that provides the
			// IsStreamOwner property on BZip2InputStream.
			switch (compression)
			{
				case Compression.BZip2:
					BZip2InputStream stream = new BZip2InputStream(inStream);
					//stream.IsStreamOwner = false;
					return stream;

				case Compression.GZip:
					GZipInputStream gzStream = new GZipInputStream(inStream);
					gzStream.IsStreamOwner = false;
					return gzStream;
			}
			throw new NotImplementedException("Algorithm not implemented.");
		}

		/// <summary>
		/// Returns a stream that will compress the data written to it.
		/// </summary>
		/// <param name="outStream">The stream that will hold the compressed data.</param>
		/// <param name="compression">The compression type to use.</param>
		/// <returns></returns>
		public static Stream GetCompressionStream(Stream outStream,
			Compression compression)
		{
			switch (compression)
			{
				case Compression.BZip2:
					return new BZip2OutputStream(outStream, 1);

				case Compression.GZip:
					return new GZipOutputStream(outStream);
			}
			throw new NotImplementedException("Algorithm not implemented.");
		}

		/// <summary>
		/// Decompresses data from one stream and writes it to another.
		/// </summary>
		/// <param name="inStream">The stream with the compressed data.</param>
		/// <param name="outStream">The stream that will hold the decompressed data.</param>
		/// <param name="compression">The compression type to use.</param>
		public static void Decompress(Stream inStream, Stream outStream,
			Compression compression)
		{
			using (Stream stream = GetDecompressionStream(inStream, compression))
			{
				WriteToStream(stream, outStream);
			}
		}

		/// <summary>
		/// Compresses data from one stream and writes it to another.
		/// </summary>
		/// <param name="inStream">The stream with the data to compress.</param>
		/// <param name="outStream">The stream that will hold the compressed data.</param>
		/// <param name="compression">The compression type to use.</param>
		public static void Compress(Stream inStream, Stream outStream,
			Compression compression)
		{
			using (outStream = GetCompressionStream(outStream, compression))
			{
				WriteToStream(inStream, outStream);
			}
		}

		/// <summary>
		/// Compresses data from one file and writes it to another.
		/// </summary>
		/// <param name="inFile">The file with the data to compress.</param>
		/// <param name="outFile">The file to write the compressed data to.</param>
		/// <param name="compression">The compression type to use.</param>
		public static void Compress(string inFile, string outFile,
			Compression compression)
		{
			using (FileStream inStream = new FileStream(inFile, FileMode.Open,
					FileAccess.Read, FileShare.Read),
				outStream = new FileStream(outFile,
					FileMode.OpenOrCreate, FileAccess.Write, FileShare.Read))
			{
				Compress(inStream, outStream, compression);
			}
		}

		private static void WriteToStream(Stream inStream, Stream outStream)
		{
			int size;
			byte[] buffer = new byte[4096];
			do
			{
				size = inStream.Read(buffer, 0, buffer.Length);
				if (size > 0)
				{
					outStream.Write(buffer, 0, size);
				}
			}
			while (size > 0);
		}

		#endregion
	}
}
